<template>
  <div class="classs-container">
    <!-- 搜索 -->
    <div class="search">
      <el-form
        :inline="true"
        :model="page"
        class="demo-form-inline"
        size="small"
        ref="Form"
      >
        <el-form-item>
          <el-input
            v-model="page.class_name"
            placeholder="班级名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="page.subject_id"
            placeholder="归属学科"
            clearable
            @focus="subjectClass"
          >
            <el-option
              v-for="item in subject"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="page.state" placeholder="显示状态">
            <el-option label="显示" value="1"></el-option>
            <el-option label="隐藏" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="getClass">搜索</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" plain @click="editClass('')"
        >创建班级</el-button
      >
    </div>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%" border>
      <el-table-column type="expand" label="#" width="50">
        <!-- 点击出现 -->
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-col :span="6">
              <el-form-item label="班级名称">
                <span>{{ props.row.class_name }}</span>
              </el-form-item></el-col
            >
            <el-col :span="18">
              <el-form-item label="归属学科">
                <span>{{ props.row.subject_name }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建人">
                <span>{{ props.row.username }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-form-item label="开始结束日期">
                <span
                  >{{ props.row.start_time | formatTimee }} 至
                  {{ props.row.end_time | formatTimee }}</span
                >
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建日期">
                <span>{{ props.row.created_at }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-form-item label="讲师">
                <span v-if="props.row.teacher_info.length">
                  <el-tag
                    type="primary"
                    disable-transitions
                    v-for="item in props.row.teacher_info"
                    :key="item.user_id"
                    :gutter="10"
                    >{{ item.username }}</el-tag
                  >
                </span>
                <span v-else>没有关联讲师</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="助教">
                <span v-if="props.row.zhujiao_info.length">
                  <el-tag
                    type="success"
                    disable-transitions
                    v-for="item in props.row.zhujiao_info"
                    :key="item.user_id"
                    :gutter="10"
                    >{{ item.username }}</el-tag
                  >
                </span>
                <span v-else>没有关联助教</span>
              </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-form-item label="班主任">
                <span v-if="props.row.manager_info.length">
                  <el-tag
                    type="warning"
                    disable-transitions
                    v-for="item in props.row.manager_info"
                    :key="item.user_id"
                    :gutter="10"
                    >{{ item.username }}</el-tag
                  >
                </span>
                <span v-else>没有关联班主任</span>
              </el-form-item>
            </el-col>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="序号" type="index" width="60"> </el-table-column>
      <el-table-column label="班级ID" prop="id" width="70"> </el-table-column>
      <el-table-column label="班级名称" prop="class_name" width="200">
      </el-table-column>
      <el-table-column label="归属学科" width="150" prop="subject_name">
      </el-table-column>
      <el-table-column label="讲师" prop="teacher_info" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.teacher_info.length">
            <el-tag
              type="primary"
              disable-transitions
              v-for="item in scope.row.teacher_info"
              :key="item.user_id"
              :gutter="10"
              >{{ item.username }}</el-tag
            >
          </span>
          <span v-else>没有关联讲师</span>
        </template>
      </el-table-column>
      <el-table-column label="助教" prop="zhujiao_info" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.zhujiao_info.length">
            <el-tag
              type="success"
              disable-transitions
              v-for="item in scope.row.zhujiao_info"
              :key="item.user_id"
              :gutter="10"
              >{{ item.username }}</el-tag
            >
          </span>
          <span v-else>没有关联助教</span>
        </template>
      </el-table-column>
      <el-table-column label="班主任" prop="manager_info" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.manager_info.length">
            <el-tag
              type="warning"
              disable-transitions
              v-for="item in scope.row.manager_info"
              :key="item.user_id"
              :gutter="10"
              >{{ item.username }}</el-tag
            >
          </span>
          <span v-else>没有关联班主任</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="username" width="140">
      </el-table-column>
      <el-table-column label="是否显示" prop="state" width="140">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.state === 1"
            active-text="是"
            inactive-text="否"
            @change="changeState(scope.row)"
          >
          </el-switch
        ></template>
      </el-table-column>
      <el-table-column
        label="开始结束日期"
        prop="start_time,end_time"
        width="240"
      >
        <template slot-scope="scope"
          >{{ scope.row.start_time | formatTimee }} 至
          {{ scope.row.end_time | formatTimee }}</template
        >
      </el-table-column>
      <el-table-column label="创建日期" prop="created_at" width="160">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            circle
            @click="editClass(scope.row.id)"
          ></el-button>
          <el-button
            @click.native.prevent="delClass(scope.row.id)"
            size="small"
            fixed
            type="danger"
            icon="el-icon-delete"
            circle
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-row type="flex" justify="end" style="height: 60px" class="fenye">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size.sync="page.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </el-row>

    <!-- 编辑的弹出框组件 -->
    <EditClass ref="edit" @update="getClass" />
  </div>
</template>

<script>
import { getClass, delClass, editClass, subjectClass } from "@/api/class";
import EditClass from "./components/editClass.vue";
export default {
  name: "Class",
  components: { EditClass },
  props: {},
  data() {
    return {
      list: [],
      list1: {},
      // state: "",
      page: {
        class_name: "",
        subject_id: "",
        state: "",
        page: 1,
        total: 0,
        limit: 10, // 页面显示条数
      },
      subject: [], // 学科
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getClass();
    this.subjectClass();
  },
  mounted() {},
  methods: {
    // 获取班级信息
    async getClass() {
      const { list, page, total } = await getClass(this.page);
      this.page.page = page;
      this.page.total = total;
      list.forEach((item) => {
        item.teacher_info = JSON.parse(item.teacher_info);
        item.manager_info = JSON.parse(item.manager_info);
        item.zhujiao_info = JSON.parse(item.zhujiao_info);
        return;
      });
      this.list = list;
      console.log("班级信息", this.list);
    },

    // 重置
    resetForm() {
      this.page.class_name = "";
      this.page.subject_id = "";
      this.page.state = "";
      this.getClass();
    },
    // 删除
    delClass(id) {
      this.$confirm("确定删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delClass(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 列表更新
          this.getClass();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page.limit = val;
      this.getClass();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.page = val;
      this.getClass();
    },
    // 编辑
    async editClass(id) {
      await this.$refs.edit.EditShowClass(id);
      this.$refs.edit.showDialogEdit = true;

      console.log("id", id);
    },
    // 状态改变
    async changeState(row) {
      await editClass({ id: row.id, state: row.state == 1 ? 2 : 1 });
      this.$message.success("状态修改成功!");
      this.getClass();
    },
    // 学科下拉菜单回显
    async subjectClass() {
      this.subject = await subjectClass();
      console.log("学科回显", this.subject);
    },
  },
};
</script>
<style  lang="scss" scoped>
.classs-container {
  background-color: #fff;
  padding: 16px;
  .search {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16px;
  }
  ::v-deep .el-table th {
    background-color: #fafafa;
    color: #646464;
    text-align: center;
  }
  ::v-deep .el-table td {
    text-align: center;
  }
  .el-tag {
    margin: 5px;
  }
  ::v-deep .el-table .demo-table-expand {
    text-align: left;
    .el-form-item__label {
      width: 120px;
      color: #99a9bf;
      font-weight: 400;
    }
    .el-col-6,
    .el-col-18 {
      height: 40px !important;
    }
  }
  .fenye {
    margin-top: 20px;
  }
}
</style>